/**
 * Created by huanggw on 2015/11/9.
 */

var userApp = angular.module('userpage',['ngAnimate']);
userApp.controller('userCtrl', function($scope, $timeout, $window, $document) {
    var float = document.getElementById("float");
    var panorama = new BMap.Panorama('map');
    panorama.setOptions({linksControl: false});
    panorama.setOptions({navigationControl: false});

    $scope.nomoment = 0;
    $scope.likenum = 0;
    $scope.itemsPerPage = 10;
    $scope.pageNum = 0;
    $scope.curPage = 1;
    $scope.totalItems = [];
    $scope.moments = [];
    $scope.reachBottom = false;
    $scope.tip = "下拉加载更多";

    var window = angular.element($window);

    var url = host+"/home";
    //获取当前主页用户
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xhr.onreadystatechange=function(){
        if(xhr.readyState==4){
            if(xhr.status==200){
                $timeout(function() {
                    var result = JSON.parse(xhr.responseText);
                    $scope.username = result.user;
                    $scope.choose(0);
                }, 20)
            }
        }
    }
    xhr.send(JSON.stringify({type: 'getUser'}));

    $scope.choose = function(num) {
        $scope.chosen = num;
        $scope.moments = [];
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if(xhr.status==200){
                    $timeout(function() {
                        var result = JSON.parse(xhr.responseText);
                        if(!result[0]) {
                            $scope.nomoment = 1;
                        }
                        else {
                            $scope.nomoment = 0;
                            $scope.totalItems = result;
                            $scope.pageNum = Math.ceil($scope.totalItems/$scope.itemsPerPage);
                            $scope.loadMore(1);
                            $scope.numbers = num==0? "共"+result.length+"条评论" : "共"+result.length+"个赞"
                        }
                    }, 20)
                }
            }
        }
        if(num == 0) {
            xhr.send(JSON.stringify({type: 'comment', user: $scope.username}));
        }else {
            xhr.send(JSON.stringify({type: 'like', user: $scope.username}));
        }
    }


    $scope.floatMap = function(event, num, type) {
        var city = $scope.moments[num].cityname;
        var school = $scope.moments[num].schoolname;
        var campus = $scope.moments[num].campusname;
        var gate = $scope.moments[num].gatename;
        $scope.getGateInfo(city, school, campus, gate)

        var left = angular.element(event.target).prop('offsetLeft');
        var width = angular.element(event.target).prop('offsetWidth');
        var top = angular.element(event.target).prop('offsetTop');
        var height = angular.element(event.target).prop('offsetHeight');

        var windowBottom = window.height() + window.scrollTop();
        //console.log(windowBottom, top+ height)

        //console.log(left, width, top)
        if(type == 1) {
            float.style.left = left + width + 5 + 'px';
        } else {
            float.style.left = left + width + 45 + 'px';
        }
        float.style.opacity = 1;
        //float.style.height = '240px';
        //float.style.width = '360px';
        if(windowBottom < top + height + 150) {
            float.style.top = top - 300 + 'px';
        }
        else {
            float.style.top = top - 152 + 'px';
        }
    }

    $scope.close = function() {
        float.style.opacity = 0;
        //float.style.height = '400px';
        //float.style.width = '600px';
        //float.style.top = '-40%';
    }

    $scope.getGateInfo = function(city, school, campus, gate) {
        var url = encodeURI(host+"/getinfo/?city="+city+"&school="+school+"&campus="+campus+'&gate='+gate);
        xhr.open("GET", url, true); //getinfo?num=32&num1=13&type=add
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if(xhr.status==200){
                    var data = JSON.parse(xhr.responseText);
                    $scope.gateinfo = data;
                    //$scope.getlike(city, school, campus, gate);
                    $timeout(function(){
                        $scope.setView(data[0].pid, data[0].heading, data[0].pitch);
                    },50)
                }
            }
        }
        xhr.send();
    }

    $scope.getlike = function(city, school, campus, gate) {
        var url = encodeURI(host+"/like/?city="+city+"&school="+school+"&campus="+campus+"&gate="+gate);
        xhr.open("GET", url, true);
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if(xhr.status==200){
                    $timeout(function(){
                        $scope.likenum = JSON.parse(xhr.responseText).likenum;
                    }, 10)
                }
            }
        }
        xhr.send();
    }

    $scope.setView = function(id, heading, pitch) {
        panorama.setId(id);
        $timeout(function(){
            panorama.setPov({heading: heading, pitch: pitch, zoom: 1.31});
        },200);
    }

    window.on('scroll', function() {
        var windowBottom = window.height() + window.scrollTop();
        var eleBottom = $document.height();
        if(windowBottom == eleBottom) {
            $scope.reachBottom = true;
            $timeout(function(){
                $scope.tip = "加载中";
                $scope.curPage += 1;
                $timeout(function(){
                    $scope.loadMore($scope.curPage)
                }, 1000)
            }, 10)
        }
    })

    $scope.checkScroll = function() {
        var windowBottom = window.height() + window.scrollTop();
        var eleBottom = $document.height();
        if(windowBottom < eleBottom) {
            $scope.shouldScroll = true;
        }
        else{
            $scope.shouldScroll = false;
        }
    }

    $scope.loadMore = function(page) {
        var start = (page-1)*15;
        var end = page*15;
        if(page == $scope.pageNum) {
            end = $scope.totalItems.length;
            $scope.tip = "已无更多数据";
        }
        var temp = $scope.totalItems.slice(start, end);
        //console.log(temp)
        $timeout(function() {
            $scope.moments = $scope.moments.concat(temp);
            //console.log($scope.moments)
        },50)
    }

    $scope.setView("01000300001309181348482385D", 247, 1, 1.31);

    window.on('load', function() {
        $scope.checkScroll()
    })
})
